<?php
  // warp player to another location
  require_once dirname(__FILE__)."/../purin2/core/core.php";
  require_once dirname(__FILE__)."/../purin2/xtr/xtr.php";
  require_once dirname(__FILE__)."/../purin2/user/session.php";
  
  puXtrBegin("warp player to another location");
  puXtrInputs(
    'v_filename',  'string',  'map xml name (e.g. maps/009-1b.tmx) where avatar will be warped',
    'i_x',         'integer',  'location on map where avatar will be warped',
    'i_y',         'integer',  'location on map where avatar will be warped'
  );
  puXtrOutputs(
    'i_tmap',      'integer',  'map where avatar is',
    'v_name',      'string',   'map name',
    'v_filename',  'string',   'map XML file name',
    'i_x',         'integer',  'location on map where avatar is',
    'i_y',         'integer',  'location on map where avatar is'
  );

  // get input parameters
  $j_user = $pu_user_serial;
  if ($j_user <= 0)
    puXtrDie(1,"You must be logged in to warp (pu_user_cid=$pu_user_cid, pu_user_csrf=$pu_user_csrf)!");
    
  // is there such map?
  $v_filename = puStr('v_filename');
  if (empty($v_filename))
    puXtrDie(6,"Undefined map xml name");
  
  $i_tmap = puSelect("select i_tmap from tmap where v_filename='$v_filename'");
  if ($i_tmap <= 0)
    puXtrDie(2,"No such map (v_filename=$v_filename)");
    
  // coordinates
  $i_x = puInt('i_x');
  $i_y = puInt('i_y');
  if ( ($i_x <= 0)||($i_y <= 0) )
    puXtrDie(3,"Invalid coordinates x=$i_x y=$i_y");

  // update position
  $sql = "update asta set j_tmap=$i_tmap, i_x=$i_x, i_y=$i_y where j_user=$j_user";
  puXtrComment($sql);
  $rows = puUpdate($sql,true);
  if ($rows < 0)
    puXtrDie(4, "Warp failed (updated rows=$rows i_tmap=$i_tmap, i_x=$i_x, i_y=$i_y, j_user=$j_user)");
  
  // send me new map info like in location.php
  $sql = "select i_tmap, v_name, v_filename, i_x, i_y from asta, tmap where i_tmap=j_tmap and j_user=$j_user";
  puXtrRowsSql($sql,5);
  puXtrEnd(0,'Successfull');
?>